package com.example.wanbaqian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.wanbaqian.R;
import com.example.wanbaqian.adapter.FindFreeAdapter;
import com.example.wanbaqian.base.BaseActivity;
import com.example.wanbaqian.config.CommonValue;
import com.example.wanbaqian.base.HttpCallback;
import com.example.wanbaqian.config.HttpURL;
import com.example.wanbaqian.base.RefreshCallback;
import com.example.wanbaqian.bean.CouponInfo;
import com.example.wanbaqian.bean.Info;
import com.example.wanbaqian.utils.GetJsonUtils;
import com.example.wanbaqian.utils.HttpUtils;
import com.example.wanbaqian.utils.JsonUtils;
import com.example.wanbaqian.utils.SPUtils;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.wrapper.EmptyWrapper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 优惠券界面
 */
public class FindFreeActivity extends BaseActivity implements HttpCallback, RefreshCallback, MultiItemTypeAdapter.OnItemClickListener {
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    private List<CouponInfo> mList = new ArrayList<>();
    private EmptyWrapper mAdapter = null;
    private FindFreeAdapter adapter;
    private static final String TAG = "FindFreeActivity";

    @Override
    protected int getLayout() {
        return R.layout.activity_findfree;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setStatusBarColor(R.color.main_color);
        setToolBar("优惠券");
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initDate() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new FindFreeAdapter(mContext, mList);
        mAdapter = new EmptyWrapper(adapter);
        mAdapter.setEmptyView(R.layout.view_empty_findfree);
        mRecyclerView.setAdapter(mAdapter);
        setHeaderAndFooter(R.id.springview, true, true, this);
        getDate();
        adapter.setOnItemClickListener(this);
    }

    private void getDate() {
        HttpUtils.getInstance().post(GetJsonUtils.getInstance()
                        .getRequest(HttpURL.MY_COUPON, mContext,
                                new String[]{"accessid", "cur_page", "page_size"}
                                , new Object[]{SPUtils.getAccessid(mContext), page,
                                        count})
                , null, CommonValue.REQUEST_CODE1, this);
    }

    @Override
    public void onSuccess(String content, Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        if (page == 1) {
            mList.clear();
        }
        canLoadmore = JsonUtils.getInstance().havePage(content);
        List<CouponInfo> newmList = JsonUtils.getInstance().getList(content, "coupon_list", CouponInfo.class);
        mList.addAll(newmList);
//        mList.add(new CouponInfo("1", "2", "2017-4-25 15:36:43", "2017-4-25 15:36:43", "1", "0"));
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onCacheSuccess(String content, Info info, int requestCode) {

    }

    @Override
    public void onError(Info info, int requestCode) {
        springView.onFinishFreshAndLoad();
        showToast(info.getMsg());
    }

    @Override
    public void getNetDate() {
        getDate();
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
        CouponInfo info = mList.get(position);
        if (info.getUse_flag().equals("1")) {
            showToast("该优惠券已使用，请勿重复使用!");
        } else if (Long.parseLong(info.getExpire_time()) < System.currentTimeMillis() / 1000) {
            showToast("该优惠券已过期，请勿使用!");
        } else {
            Intent intent = new Intent(this, BackMoneyOneActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("bean", info);
            intent.putExtras(bundle);
            setResult(RESULT_OK, intent);
            finish();
        }
    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }
}
